package site.wetsion.employ.server.controller;


import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import site.wetsion.employ.server.pojo.CollectionCompany;
import site.wetsion.employ.server.pojo.CollectionJob;
import site.wetsion.employ.server.pojo.CollectionPost;
import site.wetsion.employ.server.pojo.Company;
import site.wetsion.employ.server.pojo.ForumPost;
import site.wetsion.employ.server.pojo.Job;
import site.wetsion.employ.server.pojo.User;
import site.wetsion.employ.server.service.UserService;
import site.wetsion.employ.server.util.Page;
import site.wetsion.employ.server.util.Result;
/**
 * 用户相关的控制类，包含注册登录
 * @author Wetsion
 *
 */
@Controller
public class UserController {
	
	private final static Logger log = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private UserService userService;
	
	private final static String Portrait = "/img";
	/**
	 * 用户登录
	 * @return 首页视图
	 */
	@ResponseBody
	@PostMapping("login")
	public Result login(String email, String password, HttpSession session){
		User user = userService.login(email, password);
		if(user!=null){
			session.setAttribute("curr_user", user);
			return new Result(200, "登录成功");
		}
		return new Result(500, "登录失败，账户或密码错误");
	}
	
	/**
	 * 用户注册
	 * @return
	 */
	@ResponseBody
	@PostMapping("register")
	public Result register(int usertype, String email, 
			String code, String password, HttpSession session){
		
		System.out.println(usertype);
		System.out.println(email);
		System.out.println(code);
		System.out.println(password);
		String emailcode = (String) session.getAttribute("emailcode");
		if(!code.equals(emailcode)){
			return new Result(500, "验证码错误");
		}
		User user = new User();
		user.setEmail(email);
		user.setPassword(password);
		user.setGender(0);
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		user.setGmt_create(ts);
		user.setGmt_modified(ts);
		user.setNickname(email);
		user.setPortrait(Portrait);
		user.setUsertype(usertype);
		
		boolean rs = userService.register(user);
		if(rs){
			return new Result(200, "注册成功");
		}
		return new Result(500, "注册失败");
	}
	
	@GetMapping("logout")
	public String logout(HttpSession session, HttpServletRequest request){
		String fromUrl = request.getHeader("Referer");
		System.out.println("fromUrl:"+fromUrl);
		session.invalidate();
		return "redirect:/";
	}
	
	/** 用户收藏职位*/
	@ResponseBody
	@GetMapping("user/collect/job")
	public Result collectJob(BigInteger jobId, HttpSession session){
		User user = (User) session.getAttribute("curr_user");
		CollectionJob collection = new CollectionJob();
		collection.setCollection_job_id(jobId);
		collection.setCollection_job_user_id(user.getId());
		boolean rs = userService.addJob2Collection(collection);
		if(rs){
			return new Result(200, "收藏成功");
		}
		return new Result(400, "收藏失败");
	}
	
	/** 用户收藏公司*/
	@ResponseBody
	@GetMapping("user/collect/company")
	public Result collectCompany(BigInteger companyId, HttpSession session){
		User user = (User) session.getAttribute("curr_user");
		CollectionCompany collection = new CollectionCompany();
		collection.setCollection_company_id(companyId);
		collection.setCollection_company_user_id(user.getId());
		boolean rs = userService.addCompany2Collection(collection);
		if(rs){
			return new Result(200, "收藏成功");
		}
		return new Result(400, "收藏失败");
	}
	
	/** 用户收藏帖子*/
	@ResponseBody
	@GetMapping("user/collect/post")
	public Result collectPost(BigInteger postId, HttpSession session){
		User user = (User) session.getAttribute("curr_user");
		CollectionPost collection = new CollectionPost();
		collection.setCollection_post_id(postId);
		collection.setCollection_post_user_id(user.getId());
		boolean rs = userService.addPost2Collection(collection);
		if(rs){
			return new Result(200, "收藏成功");
		}
		return new Result(400, "收藏失败");
	}
	
	/** 帖子收藏列表*/
	@GetMapping("user/collection/post/{pageNum}")
	public String collectionPost(@PathVariable int pageNum, 
			Model model, HttpSession session){
		User user = (User) session.getAttribute("curr_user");
		Page page = new Page(pageNum, 10);
		List<ForumPost> collectionPostList = userService.getCollectionPostListByUid(user.getId(), page);
		int count = userService.getCountOfMyCollectionPost(user.getId());
		page.setTotalRecord(count);
		model.addAttribute("collectionPostList", collectionPostList);
		model.addAttribute("page", page);
		return "forumCollection";
	}
	
	/** 帖子收藏列表*/
	@GetMapping("user/collection/job/{pageNum}")
	public String collectionJob(@PathVariable int pageNum, 
			Model model, HttpSession session){
		User user = (User) session.getAttribute("curr_user");
		Page page = new Page(pageNum, 10);
		List<Job> collectionJobList = userService.getCollectionJobListByUid(user.getId(), page);
		int count = userService.getCountOfMyCollectionJob(user.getId());
		page.setTotalRecord(count);
		model.addAttribute("collectionJobList", collectionJobList);
		model.addAttribute("page", page);
		return "user/student/jobcollection";
	}
	
	/** 帖子收藏列表*/
	@GetMapping("user/collection/company/{pageNum}")
	public String collectionCompany(@PathVariable int pageNum, 
			Model model, HttpSession session){
		User user = (User) session.getAttribute("curr_user");
		Page page = new Page(pageNum, 10);
		List<Company> collectionCompanyList = userService.getCollectionCompanyListByUid(user.getId(), page);
		int count = userService.getCountOfMyCollectionCompany(user.getId());
		page.setTotalRecord(count);
		model.addAttribute("collectionCompanyList", collectionCompanyList);
		model.addAttribute("page", page);
		return "user/student/companycollection";
	}
	
	@ResponseBody
	@GetMapping("user/cancelcollect/post")
	public Result cancelCollectPost(BigInteger id, HttpSession session){
		User user = (User) session.getAttribute("curr_user");
		if(user==null){
			return new Result(405, "登录失效");
		}
		boolean rs = userService.cancelCollectPost(user.getId(), id);
		if(rs){
			return new Result(200, "取消收藏成功");
		}
		return new Result(400, "取消收藏失败");
	}
	
	@ResponseBody
	@GetMapping("user/cancelcollect/job")
	public Result cancelCollectJob(BigInteger id, HttpSession session){
		User user = (User) session.getAttribute("curr_user");
		if(user==null){
			return new Result(405, "登录失效");
		}
		boolean rs = userService.cancelCollectJob(user.getId(), id);
		if(rs){
			return new Result(200, "取消收藏成功");
		}
		return new Result(400, "取消收藏失败");
	}
	
	@ResponseBody
	@GetMapping("user/cancelcollect/company")
	public Result cancelCollectCompany(BigInteger id, HttpSession session){
		User user = (User) session.getAttribute("curr_user");
		if(user==null){
			return new Result(405, "登录失效");
		}
		boolean rs = userService.cancelCollectCompany(user.getId(), id);
		if(rs){
			return new Result(200, "取消收藏成功");
		}
		return new Result(400, "取消收藏失败");
	}
	
}
